import threading
import time


#使用 threading 模块创建线程

exitFlag = 0

class myThread(threading.Thread):
    def __init__(self,threadID, name, counter):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter

    def run(self):
        print("开始线程: " + self.name)
        #获取锁用于线程同步
        threadLock.acquire()
        print_time(self.name, self.counter, 5)
        #释放锁
        threadLock.release()
        print("退出线程: " + self.name)



def print_time(threadName, delay, counter):
    while counter:
        if exitFlag:
            threadName.exit()
        time.sleep(delay)
        print("%s: %s" %(threadName, time.ctime(time.time())))
        counter -= 1


threadLock = threading.Lock()

#创建线程
thread1 = myThread(1, "Thread-1", 30)
thread2 = myThread(2, "Thread-2", 5)


#开启新线程
thread1.start()
thread2.start()

thread1.join()
thread2.join()
print("退出主线程")